﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_TH_ProductList : System.Web.UI.UserControl
{
    private DataSet objDataSet;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadSanPhamPhoThong(500000, 2500000, "Điện thoại di động");
        LoadSanPhamTrungCap(2500000, 7000000, "Điện thoại di động");
        LoadSanPhamCaoCap(7000000, 100000000, "Điện thoại di động");
    }
    private void LoadSanPhamPhoThong(long giasan, long giatran, string loaisp)
    {
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objSanPham.SelectSanPhamByGia(giasan, giatran, loaisp);
        repSanPhamPhoThong.DataSource = objDataSet.Tables[0];
        repSanPhamPhoThong.DataBind();
    }
    private void LoadSanPhamTrungCap(long giasan, long giatran, string loaisp)
    {
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objSanPham.SelectSanPhamByGia(giasan, giatran, loaisp);
        repSanPhamTrungCap.DataSource = objDataSet.Tables[0];
        repSanPhamTrungCap.DataBind();
    }
    private void LoadSanPhamCaoCap(long giasan, long giatran, string loaisp)
    {
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objSanPham.SelectSanPhamByGia(giasan, giatran, loaisp);
        repSanPhamCaoCap.DataSource = objDataSet.Tables[0];
        repSanPhamCaoCap.DataBind();
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void repSanPhamPhoThong_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(e.CommandArgument.ToString());

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }

    protected void repSanPhamTrungCap_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(e.CommandArgument.ToString());

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }

    protected void repSanPhamCaoCap_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(e.CommandArgument.ToString());

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
}
